package mtn.bowen.yan.server.controller;

import com.jfoenix.controls.JFXComboBox;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.TextArea;
import javafx.scene.control.Toggle;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import mtn.bowen.yan.server.enums.DishesKindEnum;
import mtn.bowen.yan.server.enums.PollsSuggestEnum;
import mtn.bowen.yan.server.fx.FxmlView;
import mtn.bowen.yan.server.fx.StageManager;
import mtn.bowen.yan.server.model.PictureDishes;
import mtn.bowen.yan.server.model.PollsSuggest;
import mtn.bowen.yan.server.service.MenuManagementService;
import mtn.bowen.yan.server.service.PictureDishesService;
import mtn.bowen.yan.server.service.PollsSuggestService;
import mtn.bowen.yan.server.utils.LoadingUtils;
import mtn.bowen.yan.server.utils.Platform;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.net.URL;
import java.util.Date;
import java.util.Map;
import java.util.ResourceBundle;

/**
 * @author <a href=mailto:yanbowen90@foxmail.com> bowen.yan </a>
 * @version 1.0
 * @description: TODO
 * @date 2021-11-03 6:03 PM
 */
@Component
public class PollsSuggestController extends BaseController implements Initializable {

    private final static Logger LOGGER = LoggerFactory.getLogger(PollsSuggestController.class);

    @FXML
    private StackPane stackPane;

    @FXML
    private JFXComboBox pollsSuggestComboBox;

    @FXML
    private HBox pollsSuggestHBox;

    @Resource
    private PollsSuggestService pollsSuggestService;

    @FXML
    private TextArea suggestText;

    @Resource
    private PictureDishesService pictureDishesService;

    @FXML
    private ToggleGroup toggleGroup_praise;

    @Resource
    private LoadingUtils loadingUtils;

    @Resource
    private MenuManagementService menuManagementService;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        //选择类型
        pollsSuggestComboBox.getSelectionModel().selectFirst();

        pollsSuggestHBox.prefWidthProperty().bind(stackPane.widthProperty());
        //意见建议输入框
        suggestText.prefWidthProperty().bind(stackPane.widthProperty());
        suggestText.setFont(Font.font("KaiTi", FontWeight.NORMAL, 16));
        //按种类更新全部菜品UI
        pollsSuggestService.updatePollsSuggestUi(pollsSuggestHBox, pollsSuggestComboBox);
        StageManager.getPollsSuggestMap().clear();
    }


    /**
     * 提交投票
     */
    @FXML
    private void submitPollsSuggest() {

        int kind = pollsSuggestComboBox.getSelectionModel().getSelectedIndex() + 1;
        Map<String, String> pollsSuggestMap = StageManager.getPollsSuggestMap();
        String trim = suggestText.getText().trim();

        Toggle selectedToggle = toggleGroup_praise.getSelectedToggle();
        Integer code = Integer.valueOf(selectedToggle.getUserData().toString());
        System.out.println(code);
        PollsSuggestEnum state = PollsSuggestEnum.getPollsSuggest(code);

        if (pollsSuggestMap != null && pollsSuggestMap.size() > 0) {
            //开始执行Loading界面、并设置解除等待标记
            StageManager.putParentLoadMap("ParentLoad", loadingUtils.loading(StageManager.getParentStackPaneMap("parentStackPane")));
            StageManager.getExecutor().execute(() -> {
                Platform.runLater(() -> {
                    pollsSuggestMap.forEach((key, value) -> {
                        PictureDishes dishes = pictureDishesService.selectPictureDishesByUUid(key);
                        if (dishes != null) {
                            PollsSuggest build = PollsSuggest.builder().uuid(key).kind(kind).name(dishes.getName()).chef(dishes.getChef()).createTime(new Date()).suggest(trim).state(state.getState()).build();
                            pollsSuggestService.savePollsSuggest(build);
                        }
                    });
                    pollsSuggestService.updatePollsSuggestUi(pollsSuggestHBox, pollsSuggestComboBox);
                    StageManager.getPollsSuggestMap().clear();
                    loadingUtils.remove(StageManager.getParentStackPaneMap("parentStackPane"), StageManager.getParentLoadMap("ParentLoad"));
                    menuManagementService.showDialog("投票成功", "【" + DishesKindEnum.getDishesKind(kind).getMsg() + "】 【投票】 成功！", Alert.AlertType.INFORMATION);
                });
            });
        }

    }

    @FXML
    private void pollsSuggest() {
        StageManager.getPollsSuggestMap().clear();
        //按种类更新全部菜品UI
        pollsSuggestService.updatePollsSuggestUi(pollsSuggestHBox, pollsSuggestComboBox);
    }
}
